package com.shengdou.as.slopsapptest.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseActivity;
import com.shengdou.as.slopsapptest.bean.ManagerInfo;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.HttpUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.NetConstant;
import com.shengdou.as.slopsapptest.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 添加管理列表
 */
public class ManagerAddActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener {

    private LinearLayout mLinear;
    private ListView mListView;
    private ManagerInfo user;
    private LinearLayout gone;
    private ArrayList<ManagerInfo.DataBean> arrayList;
    private Map<String, String> params = new HashMap<>();
    private MyAdapter adapter;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constants.USERDEL:
                    String result = msg.obj.toString();
                    ToastUtils.showToast(result);
                    params.clear();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        ToastUtils.showToast(jsonObject.getString("status_text"));
                        HttpPost();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case Constants.USERLIST:
                    String results = msg.obj.toString();
                    LogUtil.e("result", results);
//                    arrayList.clear();
                    ManagerInfo managerInfo = new Gson().fromJson(results, ManagerInfo.class);
                    arrayList = managerInfo.getData();
                    LogUtil.e("arraylist.size", arrayList.size() + "");
                    for (int i = 0; i < arrayList.size(); i++) {
                        LogUtil.e(arrayList.get(i).getName(), arrayList.get(i).getName());
                    }
                    for (int i = 0; i < managerInfo.getData().size(); i++) {
                        LogUtil.e("managerInfo.getData()--" + managerInfo.getData().get(i).getName(), managerInfo.getData().get(i).getName());
                    }
                    if (arrayList != null && arrayList.size() > 0) {
                        mListView.setVisibility(View.VISIBLE);
                        gone.setVisibility(View.GONE);
                        mListView.setAdapter(adapter);
                        adapter.setmArray(arrayList);
                    } else {
                        adapter.notifyDataSetChanged();
                        mListView.setVisibility(View.GONE);
                        gone.setVisibility(View.VISIBLE);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_add);
        init();
    }

    private void init() {
        mLinear = (LinearLayout) findViewById(R.id.Linear);
        mLinear.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.listview);
        gone = (LinearLayout) findViewById(R.id.txt_gone);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.e("=============", "onResume");
        HttpPost();
        mListView.setOnItemLongClickListener(this);
    }

    private void HttpPost() {
        arrayList = new ArrayList<>();
        adapter = new MyAdapter();
        HttpUtils.posts(mHandler, NetConstant.USERLIST(), Constants.USERLIST, ManagerAddActivity.this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Linear:
                startActivity(new Intent(ManagerAddActivity.this, SetManagerActivity.class));
                break;
        }
    }

    public void orderClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        final AlertDialog alertDialog = new AlertDialog.Builder(ManagerAddActivity.this).create();
        alertDialog.show();
        alertDialog.getWindow().setContentView(R.layout.dialog_dele);
        /*alertDialog.getWindow().findViewById(R.id.dialog_verify_cancle)
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        alertDialog.cancel();
                    }
                });*/
        alertDialog.getWindow().findViewById(R.id.dialog_verify_go)
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        alertDialog.cancel();
                        HttpDeletePost(arrayList.get(position).getId());

                    }
                });
        return true;
    }

    private void HttpDeletePost(int id) {
        params.put("uid", id + "");
        HttpUtils.post(mHandler, NetConstant.USERDEL(), params, Constants.USERDEL, ManagerAddActivity.this);
    }

    class MyAdapter extends BaseAdapter {
        private ArrayList<ManagerInfo.DataBean> mArray;

        public MyAdapter() {
        }

        public void setmArray(ArrayList<ManagerInfo.DataBean> mArray) {
            this.mArray = mArray;
        }

        @Override
        public int getCount() {
            return mArray != null ? mArray.size() : 0;
        }

        @Override
        public ManagerInfo.DataBean getItem(int position) {
            return mArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(ManagerAddActivity.this, R.layout.item_manager_list, null);
                holder.img = (LinearLayout) convertView.findViewById(R.id.img_edit);
                holder.mTxtName = (TextView) convertView.findViewById(R.id.txt_name);
                holder.mTxtPhone = (TextView) convertView.findViewById(R.id.txt_phone);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            LogUtil.e(mArray.get(position).getName(), mArray.get(position).getMobile());
            holder.mTxtName.setText(mArray.get(position).getName());
            holder.mTxtPhone.setText(mArray.get(position).getMobile());
            final int index = position;
            holder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ManagerAddActivity.this, SetManagerActivity.class);
                    intent.putExtra("id", mArray.get(index).getId());
                    intent.putExtra("name", mArray.get(index).getName());
                    intent.putExtra("mobile", mArray.get(index).getMobile());
                    intent.putExtra("factory", mArray.get(index).getFactory());
                    startActivity(intent);
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        LinearLayout img;
        TextView mTxtName;
        TextView mTxtPhone;
    }
}